﻿@using Newtouch.Infrastructure
<link href="~/Content/css/beveltabs.css" rel="stylesheet" />
<script src="~/Content/js/beveltabs.js"></script>
<div id="linkxmlr" role="tabpanel" class="tab-pane" style="width:98%; margin-right: 10px;margin-top: 3px;">
    <div class="contentPanel" style="border: 1px solid #ddd;">
        <div style="background-color: #fff;" id="dv_gridsfxm">
            <table id="gridsfxm" style="border-collapse:collapse"></table>
            <div id="gridsfxmPager"></div>
        </div>
    </div>
    <div class="toolbar" style="float:right;width:80%;margin:10px 30px 12px;text-align:right;">
        <a class="btn btn-primary" style="margin-left:4px;" onclick="HistoricalOrders(@Html.Raw(((int)EnumYzlx.sfxm).ToString()))">历史医嘱引用</a>
        <a class="btn btn-primary" style="margin-left:4px;" id="btn_bottombutton_f7" onclick="ReviewToday()">当日查看</a>
        <a class="btn btn-primary" style="margin-left:4px;" id="btn_bottombutton_f8" onclick="SaveItemTemplate()">另存为套餐</a>
        <a class="btn btn-primary" style="margin-left:4px;" id="btn_bottombutton_f9" onclick="SaveItemMedicine('1')">保存</a>
    </div>
</div>

@*项目录入*@
<script>
    //var localdata = [];//带药对象
    var deldata = [];//删除对象
    var zlltext = ('@ViewBag.zzlconfig' == "" ? "每次治疗量" : '@ViewBag.zzlconfig');
    var xmyzflag = 0;

    var itemlocaldata = new Array();
    function init_ItemPrescription() {

        //仅尚未保存到数据库的医嘱需要初始化在grid中
        itemlocaldata = new Array();
        if (window.alldataArray.xmyz) {
            itemlocaldata = $.jsonWhere($.deepClone.clone(window.alldataArray.xmyz), function (icf) {
                return !!!icf.Id;
            });
            $.each(itemlocaldata, function () {
                if (!this.Id) {
                    $.modalAlert('医嘱异常', "warning");
                    location.href = location.href;
                }
                this.action = getIActionStr()
            });
        }
        else {
            window.alldataArray.xmyz = new Array(); //方便后面使用$.each()
        }

        if (xmyzflag == 0) {   //该页面初始化
            gridsfxm();
            xmyzflag = 1;
        }

        else {
            $("#gridsfxm").clearGridData(); //先清
            //再次打开该页面
            $("#gridsfxm").jqGrid('setGridParam', {
                datatype: 'local',
                data: itemlocaldata
            }).trigger("reloadGrid");
        }
    }

    //药品列表
    function gridsfxm() {
        var $gridsfxm = $("#gridsfxm");
        $gridsfxm.jqGrid({
            datatype: 'local',
            data: itemlocaldata,
            height: $(window).height() - 180,
            shrinkToFit: true,   //true:按比例初始化列宽度 false:使用colModel指定的宽度
            autowidth: true,
            rownumbers: true,  //是否显示序号
            editurl: "clientArray",  //行编辑不向服务器提交数据
            unwritten: false,
            colModel: [
                { label: 'Id', name: 'Id', align: 'center', hidden: true },
                { label: '长<br>临', name: 'yzlb', width: 35, editwidth: '100%', align: 'center', editable: true },
                {
                    label: '<span class="required">*</span>收费项目', name: 'xmmc', width: 220, editwidth: '100%', align: 'center', editable: true
                },
                {
                    label: 'zxksdm', name: 'zxksdm', editwidth: '100%', align: 'center', editable: true, hidden: true
                },
				{
				    label: '<span class="required">*</span>执行科室', name: 'zxksmc', width: 180, editwidth: '100%', align: 'center', editable: true
				},
                {
                    label: '<span class="required">*</span>开始时间', name: 'kssj', width: 135, editwidth: '100%',
                    align: 'center',
                    editable: true, unformat: pickDate, formatter: "date",
                    formatoptions: { srcformat: 'Y-m-d h:m:s', newformat: 'Y-m-d h:m:s' },
                    formatter: function (cellvalue, options, cell) {
                        return (typeof (cellvalue) == "undefined") ? "" : cellvalue;
                    }
                },
                { label: '<span class="required">*</span>单位', name: 'dw', width: 55, editwidth: '100%', align: "center", editable: true },
                { label: 'xmdm', name: 'xmdm', width: 100, editwidth: '100%', align: 'center', editable: true, hidden: true },
                { label: 'dwjls', name: 'dwjls', editwidth: '100%', align: 'center', editable: true, hidden: true },
                { label: 'dwwwwwww', name: 'dwwwwwww', width: 40, editwidth: '', align: 'center', editable: true, hidden: true },  //不可去掉
                {
                    label: '<span class="required">*</span>' + zlltext, name: 'ypjl', width: 80, editwidth: '100%', align: 'center', editable: true,
                    editoptions: {
                        dataEvents: [
                            {
                                type: 'change',
                                fn: function (e) {
                                    var cellval = $(this).val();
                                    if (cellval.replace(/(^\s*)|(\s*$)/g, "") == "") {
                                        $.modalAlert(zlltext+"为空，请确认。", 'warning');
                                        return;
                                    }
                                    if (isNaN(cellval)) {
                                        $.modalAlert(zlltext +"：请填写数字", 'warning');
                                        $(this).val('');
                                        return;
                                    }
                                    if (parseInt(cellval)<0) {
                                        $.modalAlert(zlltext + "：大于0", 'warning');
                                        $(this).val('');
                                        return;
                                    }
                                    var row = $(e.target).closest('tr.jqgrow');
                                    var rowid = row.attr('id');
                                    calitemsl(rowid);
                                }
                            }
                        ]
                    }
                },
                { label: '<span class="required">*</span>频次', name: 'pcmc', width: 50, editwidth: '100%', align: 'center', editable: true },
                { label: 'pcCode', name: 'pcCode', editwidth: '100%', align: 'center', editable: true, hidden: true },
                { label: 'zxcs', name: 'zxcs',editable: true,hidden: true },
                { label: 'zxzq', name: 'zxzq',editable: true,hidden: true },
                { label: 'zxzqdw', name: 'zxzqdw', editable: true, hidden: true },
                { label: '天数', name: 'ts', width: 50, editwidth: '100%', align: 'center', editable: true },
                { label: '部位', name: 'bw', width: 150, editwidth: '100%', align: "center", editable: true, hidden: '@ViewBag.bwhide' =="False" ? undefined : true },
                { label: '总量', name: 'sl', editwidth: '100%', hidden: true, align: 'center', editable: true},
                { label: '嘱托', name: 'ztnr', width: '@ViewBag.bwhide'=="False" ? 150 : 300, editwidth: '100%', align: 'center', editable: true },
                {
                    label: '转自费', name: 'iszzf', width: 50, editwidth: '100%', align: 'center', edittype: "select", editable: true, editoptions: {
                        dataEvents: [
                            {
                                type: 'change',
                                fn: function (e) {
                                    var row = $(e.target).closest('tr.jqgrow');
                                    var rowid = row.attr('id');
                                    $("#" + rowid + "_iszzffffff").val($(this).val());
                                }
                            }
                        ]

                    }
                },
                { label: 'iszzffffff', name: 'iszzffffff', editwidth: '', align: 'center', editable: true, hidden: true },
                {
                    label: '计费', name: 'isjf', width: 40, editwidth: '100%', align: 'center', edittype: "select", editable: true, editoptions: {
                        dataEvents: [
                            {
                                type: 'change',
                                fn: function (e) {
                                    var row = $(e.target).closest('tr.jqgrow');
                                    var rowid = row.attr('id');
                                    $("#" + rowid + "_isjffffff").val($(this).val());
                                }
                            }
                        ]

                    }
                },
                { label: 'isjffffff', name: 'isjffffff', editwidth: '', align: 'center', editable: true, hidden: true },
                { label: '操作', name: 'action', width: 80, align: 'center' },
                { name: 'yzlx', hidden: true },
                { name: 'dwlb', hidden: true },
                { name: 'zyh', hidden: true },
                { name: 'ysgh', hidden: true },
                { name: 'sfxmzt',hidden:true}
            ],
            editinputwidthborder: false,    //是否需要边框 默认为true
            editinputborderradius: false,   //是否需要边框圆角 默认true（有圆角）
            pager: "gridsfxmPager",
            gridComplete: function () {
                EnableIInlineEditBox();
                //隐藏grid底部滚动条
                $gridsfxm.closest(".ui-jqgrid-bdiv").css({ "overflow-x": "hidden" });
            }
        });

        //二级菜单
        $gridsfxm.jqGrid('setGroupHeaders', {
            useColSpanStyle: true,
            groupHeaders: [
                {
                    startColumnName: 'xmmc',
                    numberOfColumns: 4,
                    titleText: '基本信息'
                }, {
                    startColumnName: 'dwjls',
                    numberOfColumns: 7,
                    titleText: '项目用法'
                }]
        });
        $.each($(".jqg-first-row-header").find("th"),
            function () {
                $(this).css("padding", 0);
            });

        //自定义按钮
        $gridsfxm.navGrid('#gridsfxmPager',
                {
                    edit: false,
                    add: false,
                    del: false,
                    search: false,
                    refresh: false,
                    view: false,
                    position: "left",
                    cloneToTop: false
                })
            .navButtonAdd('#gridsfxmPager',
                {
                    buttonicon: "glyphicon glyphicon-new-window",
                    title: "新医嘱",
                    caption: "新医嘱",
                    position: "last",
                    onClickButton: function() {
                        newIPresData(null);
                    }
            });

        $("#gridsfxmPager_right").append("<div class=\"ckbox\" style=\"margin-top:2px;\"><input id=\"chkItemlsyz\" name=\"chkItemlsyz\" type=\"checkbox\" ><label for=\"chkItemlsyz\">临时医嘱</label></div>");
    }

    //启用行内编辑
    function EnableIInlineEditBox() {
        var ids = $("#gridsfxm").getDataIDs();
        $.each(ids, function () {
            var rowid = String(this);
            //打开编辑模式
            $("#gridsfxm").jqGrid('editRow', rowid, false, initIInlineFunc);
            $("#" + rowid + "_kssj").css("text-align", "left");

            //医嘱类别 长、临
            if ($("#" + rowid + "_yzlb").val() == undefined || $("#" + rowid + "_yzlb").val() === "") {
                var yzlb = $("#chkItemlsyz").is(":checked") ? "临" : "长";
                $("#" + rowid + "_yzlb").val(yzlb);
            }
            $("#" + rowid + "_yzlb").attr('readonly', 'true');
        });

    }

    //初始化 浮层
    function initIInlineFunc(rowid) {
        //部分列只读
        $("#" + rowid + "_pcmc").attr('readonly', 'true');
        $("#" + rowid + "_sl").css('background-color', '#f6f7fb').attr('readonly', 'true');
        $("#" + rowid + "_dw").css('background-color', '#f6f7fb').attr('readonly', 'true');
        $("#" + rowid + "_xmmc").attr('autocomplete', 'off');

        $("#" + rowid + "_ztnr").parent().next().children('select').html('');
        $("#" + rowid + "_ztnr").parent().next().children('select').append('<option value="1">是</option><option value="0">否</option>');
        $("#" + rowid + "_iszzffffff").parent().next().children('select').html('');
        $("#" + rowid + "_iszzffffff").parent().next().children('select').append('<option value="1">是</option><option value="0">否</option>');
        
        var iszzfval = $("#" + rowid + "_iszzffffff").val();
        if (!!iszzfval) {
            $("#" + rowid + "_ztnr").parent().next().children('select').val(iszzfval).trigger("change");
        } else {
            $("#" + rowid + "_ztnr").parent().next().children('select').val(0).trigger("change");
        }
        var isjfval = $("#" + rowid + "_isjffffff").val();
        if (!!isjfval) {
            $("#" + rowid + "_iszzffffff").parent().next().children('select').val(isjfval).trigger("change");
        } else {
            $("#" + rowid + "_iszzffffff").parent().next().children('select').val(1).trigger("change");
        }

        var currlineId = $("#gridsfxm").getRowData(rowid).Id;
        if (currlineId.indexOf("0.") == '-1') {//修改的数据，不允许修改收费项目
            $("#" + rowid + "_xmmc").css('background-color', '#f6f7fb').attr('readonly', 'true');
        } else {
            var searchType = "sfxm";
            var sfdllx = "RegularItem";
            if ('@ViewBag.sfxmService'=="kf") {
                searchType = "sfxm.dwjls";
                sfdllx = "Rehab";
            }
            
            //医嘱类别 浮动
            $("#" + rowid + "_yzlb").yzlbFloatingSelector({
                isinputchangetriggered: false,
                showtext: 'Name',
                attrcols: ['yzlb',"Name"],
                itemdbclickhandler: function ($this) {
                    $("#" + rowid + "_yzlb").val($this.attr('data-yzlb'));
                    $("#" + rowid + "_yzlb").trigger("change");
                }
            });

            $("#" + rowid + "_yzlb").change(function() {
                if ($("#" + rowid + "_yzlb").val() === "临") {
                    $("#" + rowid + "_sl").show();
                    $("#" + rowid + "_ts").css('background-color', '#f6f7fb').attr('readonly', 'true'); //临时医嘱 禁用天数
                    $("#" + rowid + "_sl").css('background-color', '#fff').removeAttr('readonly'); //临时医嘱 启用总量
                } else {
                    $("#" + rowid + "_ts").css('background-color', '#fff').removeAttr('readonly'); //长期医嘱 启用天数
                    $("#" + rowid + "_sl").css('background-color', '#f6f7fb').attr('readonly', 'true'); //长期医嘱 禁用总量
                    $("#" + rowid + "_sl").hide();
                }
            });

            //药品浮层  //中药西药 会重复么 ‘"#" + rowid + "_xmmc"’
            $("#" + rowid + "_xmmc").sfxmFloatingSelector({
                djDecimalPlaces: 4,
                //leftshift: 150,
                showgg: true,
                showypkc: true, //是否显示库存
                searchType: searchType,
                ajaxparameters: function ($thisinput) {
                    return "mzzybz=2&dllb=2&sfdllx=" + sfdllx+"&keyword=" + $.trim($thisinput.val());
                },
                itemdbclickhandler: function ($this) {
                    $("#" + rowid + "_xmmc").val($this.attr('data-sfxmmc'));
                    $("#" + rowid + "_xmdm").val($this.attr('data-sfxmCode'));
                    $("#" + rowid + "_dw").val($this.attr('data-dw'));
                    $("#" + rowid + "_dwjls").val($this.attr('data-dwjls'));
                    $("#" + rowid + "_zxksdm").val($this.attr('data-zxks'));
                    $("#" + rowid + "_zxksmc").val($this.attr('data-zxksmc'));
                    $("#" + rowid + "_xmmc").css('background-color', '#f6f7fb').attr('readonly', 'true');
                    var rowData = $("#gridsfxm").getRowData(rowid);
                    $("#gridsfxm").jqGrid('setCell',rowid,"yzlx",@Html.Raw(((int)EnumYzlx.sfxm).ToString()));
                    if($this.attr('data-sfdlcode')=="13")//手术大类处理
                    {
                        $("#gridsfxm").jqGrid('setCell',rowid,"yzlx","9");//设置医嘱类型为手术医嘱
                    }
                    if($this.attr('data-zfxz')=="9")//暂用此字段区分项目组套
                    {
                        $("#gridsfxm").jqGrid('setCell',rowid,"sfxmzt","Y");//项目组合
                    }
                    var jldw = $this.attr('data-jldw');    //剂量单位
                    var dw = $this.attr('data-dw');     //住院单位
                    //初始化单位和单位类别的值，否则没有值传到后台
                    //默认剂量单位，不存在剂量单位，默认住院单位，都不存在，不赋值
                    if (!!jldw) {
                        $("#" + rowid + "_dwwwwwww").val(jldw);
                        $("#" + rowid + "_dwlb").val("1");
                    } else if (!!dw) {
                        $("#" + rowid + "_dwwwwwww").val(dw);
                        $("#" + rowid + "_dwlb").val("4");
                    }
                }
            });
        }
        //频次浮层
        $("#" + rowid + "_pcmc").pcFloatingSelector({
            showtext: 'yzpcmc',
            attrcols: ['yzpcmc', 'yzpcCode', 'zxcs', 'zxzq', 'zxzqdw'],
            itemdbclickhandler: function ($this) {
                $("#" + rowid + "_pcmc").val($this.attr('data-yzpcmc'));
                $("#" + rowid + "_pcCode").val($this.attr('data-yzpcCode'));
                $("#" + rowid + "_zxcs").val($this.attr('data-zxcs'))
                $("#" + rowid + "_zxzq").val($this.attr('data-zxzq'))
                $("#" + rowid + "_zxzqdw").val($this.attr('data-zxzqdw'));
                if($("#" + rowid + "_pcmc").val()=="ST")
                {
                    $("#" + rowid + "_yzlb").val("临");
                }
                //临时医嘱天数禁用
                var pcCode = $("#" + rowid + "_pcCode").val();
                if ('@ViewBag.pcStr' !== '' && pcCode !== '' && (!isNaN(pcCode)) &&
                    (('@ViewBag.pcStr').indexOf(pcCode) > -1)) {
                    $("#" + rowid + "_ts").css('background-color', '#f6f7fb').attr('readonly', 'true');
                } else
                {
                    $("#" + rowid + "_ts").css('background-color', 'white').removeAttr('readonly');
                }
                calitemsl(rowid);
            }
        });

        $("#" + rowid + "_ypjl").keyup(function () {
            calitemsl(rowid);
        });
        $("#" + rowid + "_zxksmc").newtouchBatchFloatingSelector({
            height: 170,
            width: 300,
            caption: "选择科室",
            url: '/Rehab/GetSfxmZxksSelectJson2',
            clickautotrigger: true,
            ajaxparameters: function ($thisinput) {
                return 'sfxmCode=' + $("#" + rowid + "_xmCode").val() + "&keyword=" + $thisinput.val();
            },
            colModel: [
				{ label: '编号', name: 'Code', widthratio: 40 },
				{ label: '名称', name: 'Name', widthratio: 60 },
				{ label: '拼音', name: 'py', hidden: true }
            ],
            itemdbclickhandler: function ($thistr) {
                $("#" + rowid + "_zxksdm").val($thistr.find("td:eq(0)").html());
                $("#" + rowid + "_zxksmc").val($thistr.find('td:eq(1)').html());
                return;
            },
        });

        //部位浮层
        $("#" + rowid + "_bw").bwFloatingSelector({
            showtext: 'bwmc',
            attrcols: ['bwmc', 'bwCode'],
            checkItemActivity: function ($li, $thisinput) {
                var selected = false;//默认没有选中当前值
                var selectval = $thisinput.val();
                if (selectval) {
                    selectval = selectval.split(',');
                    $.each(selectval, function () {
                        if (this == $li.html()) {
                            selected = true;
                        }
                    });
                }
                return selected;
            },
            ajaxparameters: function ($thisinput) {
                return "keyword=" + $thisinput.val();
            },
            itemdbclickhandler: function ($this) {
                var mcval = "";
                if (typeof ($this) !== "undefined") {
                    $.each($this, function () {
                        mcval += this.attr('data-bwmc') + ",";

                    });
                    if (mcval.length > 0) {
                        mcval = mcval.substring(0, mcval.length - 1);
                    };
                }
                $("#" + rowid + "_bw").val(mcval);
            }
        });

        //嘱托浮层
        $("#" + rowid + "_ztnr").ztFloatingSelector({
            height: 100,
            itemdbclickhandler: function ($this) {
                $("#" + rowid + "_ztnr").val($this.attr('data-ztmc'));
            }
        });
    }

    function calitemsl(rowid) {
        //单位计量数/每次治疗量*频次
        var dwjls = $("#" + rowid + "_dwjls").val();
        var mczll = $("#" + rowid + "_ypjl").val();
        var zxcs = $("#" + rowid + "_zxcs").val();
        var zxzq = $("#" + rowid + "_zxzq").val();
        var zxzqdw = $("#" + rowid + "_zxzqdw").val();
        if (isNaN(dwjls) || dwjls == "") {
            return;
        } else if (isNaN(mczll) || mczll == "") {
            return;
        } else if (isNaN(zxcs) || zxcs == "") {
            return;
        } else if (isNaN(zxzq) || zxzq == "") {
            return;
        } else if (isNaN(zxzqdw) || zxzqdw == "") {
            return;
        } else if (mczll !== "" && dwjls !== "" && (parseInt(dwjls) > parseInt(mczll))) {
            //rowid = rowid.substring(3);
            $.modalAlert($("#" + rowid + "_xmmc").val()+"的" + zlltext + "不能小于单位计量数", "warning");
            $("#" + rowid + "_sl").val("");
            return false;
        } else {
            var sl = getItemsl(dwjls, mczll, zxcs, zxzq, zxzqdw);
            $("#" + rowid + "_sl").val(sl);
        }
        return true;
    }

    function calitemslByobj(obj) {
        if (isNaN(obj.dwjls) || obj.dwjls === "") {
            return false;
        } else if (isNaN(obj.ypjl) || obj.ypjl === "") {
            return false;
        } else if (isNaN(obj.zxcs) || obj.zxcs === "") {
            return false;
        } else if (isNaN(obj.zxzq) || obj.zxzq === "") {
            return false;
        } else if (isNaN(obj.zxzqdw) || obj.zxzqdw === "") {
            return false;
        } else if (obj.ypjl !== "" && obj.dwjls !== "" && (parseInt(obj.dwjls) > parseInt(obj.ypjl))) {
            //rowid = rowid.substring(3);
            $.modalAlert(obj.xmmc+"的" + zlltext + "不能小于单位计量数", "warning");
            return false;
        } else {
            var sl = getItemsl(obj.dwjls, obj.ypjl, obj.zxcs, obj.zxzq, obj.zxzqdw);
            return sl;
        }
    }

    function getItemsl(dwjls, mczll, zxcs, zxzq, zxzqdw) {
        var qzint = Number(getpcsl(zxcs, zxzq, zxzqdw));
        return qzint * Math.floor(mczll / dwjls);
    }
    //新医嘱 按钮
    function newIPresData(currentObj) {
        //if (yjjzhmsg)
        //{
        //    $.modalAlert(yjjzhmsg, 'warning');
        //    return;
        //}
        if (patobjValidate()) {
            var dataRow = {
                Id: Math.random().toString() + new Date().getMilliseconds(),
                action: getIActionStr(),
                kssj: $.getTime(),
                yzlx: @Html.Raw(((int)EnumYzlx.sfxm).ToString()),//项目录入
                yzzt: @Html.Raw(((int)EnumYzzt.Ds).ToString()),//默认未审
                zyh: currentobj.zyh,
                yzlb: $("#chkItemlsyz").is(":checked") ? "临" : "长"
            };
            var rowIds = $("#gridsfxm").jqGrid('getDataIDs');
            if (rowIds == null || rowIds.length === 0 || currentObj == null) {
                $("#gridsfxm").jqGrid("addRowData", undefined, dataRow, "last");
            } else {
                var curRowId = $(currentObj).parent().parent()[0].id;
                $("#gridsfxm").jqGrid("addRowData", undefined, dataRow, "after", curRowId);
            }
        }
    }

    //删除明细
    function deleteItemRowData(selRowId, recalc) {
        if (!!selRowId) {
           var Id= $("#gridsfxm").jqGrid('getRowData', selRowId).Id;
            $("#gridsfxm").jqGrid("delRowData", selRowId);
            if (Id.indexOf("0.")==='-1') {//记录下来，从数据库删除
                deldata.push(Id);
            }
        }
    }

    function getIActionStr() {
        return "<i class='fa fa-plus-square-o' style='font-size: large; color: #09a3ea;' onclick='newIPresData(this)'></i>&nbsp;&nbsp;&nbsp;<i class='fa fa-minus-square-o' style='font-size: large; color: #09a3ea;' onclick='deleteItemRowData($(this).parent().parent().attr(\"id\"));return false;'></i>";
    }

    //保存按钮动作
    function SaveItemMedicine(savetodb) {
        //if (yjjzhmsg)
        //{
        //    $.modalAlert(yjjzhmsg, 'warning');
        //    return;
        //}
        patobjValidate();
            //获取所有行Id，遍历使编辑框处于保存状态
            var rowIds = $("#gridsfxm").jqGrid('getDataIDs');
            for (var i = 0; i < rowIds.length; i++) {
                    var saveResult = $("#gridsfxm").saveRow(rowIds[i], null, null, null, function (callbackRowId) { }, null, null);
                    if (!saveResult) {
                        EnableIInlineEditBox();    //重启启用编辑 否则下次Save时会返回false
                        return;   //保存失败，则return
                    }
            }
            var gridsfxmData = $("#gridsfxm").jqGrid('getRowData_AllLine', null, true);
            if (gridsfxmData.length<1) {
                $.modalAlert("当前没有保存的医嘱内容", 'warning');
            }
        var gridSimpleData = [];
        var flag = false;
            $.each(gridsfxmData, function () {    //去掉action
                for (var i = 0; i < $(this).length; i++) {
                    delete $(this)[i].action;
                    $(this)[i].ysgh = '@ViewBag.ysgh';
                    var sl = calitemslByobj(this);
                    if (!sl && savetodb == '1') {
                        $.modalAlert("计算数量失败", "warning");
                        flag = true;
                        return false;
                    } else if (sl) {
                        $(this)[i].sl = sl;
                    }
                    if (savetodb == '1') {
                        if (this.xmmc == undefined || this.xmmc == "") {
                            $.modalAlert("缺少项目", "warning");
                            flag = true;
                            return false;
                        } else if (this.zxksmc == undefined || this.zxksmc == "" || this.zxksdm == undefined || this.zxksdm == ""){
                            $.modalAlert("缺少执行科室,请选择", "warning");
                            flag = true;
                            return false;
                        }else if (this.kssj == undefined || this.kssj == "") {
                            $.modalAlert("缺少开始时间", "warning");
                            flag = true;
                            return false;
                        } else if (this.kssj < window.currentobj.ryrq) {
                            $.modalAlert("[" + this.xmmc + "]开始时间不能早于入院时间 入院日期：" + window.currentobj.ryrq, "warning");
                            flag = true;
                            return false;
                        } else if (this.dw == undefined || this.dw == "") {
                            $.modalAlert("缺少单位", "warning");
                            flag = true;
                            return false;
                        } else if (this.ypjl == undefined || this.ypjl == "") {
                            $.modalAlert("缺少剂量", "warning");
                            flag = true;
                            return false;
                        }else if (this.pcmc == undefined || this.pcmc == "") {
                            $.modalAlert("缺少频次", "warning");
                            flag = true;
                            return false;
                        }
                    }
                }
        });

        if (flag) {
            EnableIInlineEditBox();
            return;
        }

        //保存数据
        window.alldataArray.xmyz = $.jsonWhere(window.alldataArray.xmyz, function (iyzmx) {
            if (!!!iyzmx.Id) {
                return false;   //编辑列表里有
            }
            for (var iIndex = 0; iIndex < gridsfxmData.length; iIndex++) {
                if (gridsfxmData[iIndex].Id == iyzmx.Id) {
                    return false;
                }
            }
            return true;
        });

        $.each(gridsfxmData, function () {
            window.alldataArray.xmyz.unshift(this);
        });

        itemlocaldata = new Array();


        if (savetodb=='1') {
            submitService(gridsfxmData);
            window.alldataArray.xmyz = [];
        }
    }
    //验证
    function ValidateI() {
        patobjValidate();
        //是否有数据
        var data = $("#gridsfxm").jqGrid('getRowData_AllLine', null, true);
        if (data.length===0) {
            $.modalAlert("缺少医嘱数据", 'warning');
            return false;
        }
        return true;
    }

    //修改操作时初始化
    function EditIInit(zyh, yzId, yzlb) {
        $.najax({
            url: "/DoctorManage/Medicine/GetYZDetail",
            dataType: "json",
            data: { zyh: zyh, yzId: yzId, yzlx: yzlb },//yzlx:长临记号
            type: "POST",
            success: function (data) {
                currentobj = data.patientInfo;
                itemlocaldata = data.DoctorServiceUIRequestDto;
                $.each(itemlocaldata, function () {
                    this.action = getIActionStr()
                    this.dw=this.dwwwwwww;
                    //this.dwjls=this.sl;
                });
                $("#gridsfxm").jqGrid('setGridParam', {
                    datatype: 'local',
                    data: itemlocaldata,
                }).trigger("reloadGrid");

            }
        });
    }

    //另存为模板
    function SaveItemTemplate() {
        $.modalOpen({
            id: "Form",
            title: "套餐",
            url: "/DoctorManage/DoctorsAdvice/Form",
            width: "400px",
            height: "300px",
            callBack: function (iframeId) {
                var obj = top.frames[iframeId].submitForm();
                if (obj && obj.mbmc != "" && obj.mblx) {
                    $.modalClose("Form");
                } else {
                    $.modalAlert("套餐名称和套餐范围必填", "warning");
                    return;
                }
                var mbObj = {};
                if (obj != typeof (undefined)) {
                    mbObj.tcfw = obj.mblx;
                    mbObj.tcmc = obj.mbmc;
                    mbObj.tclx = @Html.Raw(((int)EnumYzlx.sfxm).ToString());
                    mbObj.ysgh = '@ViewBag.ysgh';
                }
                //获取所有行Id，遍历使编辑框处于保存状态
                var rowIds = $("#gridsfxm").jqGrid('getDataIDs');
                for (var i = 0; i < rowIds.length; i++) {
                    calitemsl(rowIds[i]);
                    var saveResult = $("#gridsfxm").saveRow(rowIds[i], null, null, null, function (callbackRowId) { }, null, null);

                    if (!saveResult) {
                        EnableIInlineEditBox();    //重启启用编辑 否则下次Save时会返回false
                        return;   //保存失败，则return
                    }
                }
                var gridsfxmData = $("#gridsfxm").jqGrid('getRowData_AllLine', null, true);
                if (gridsfxmData.length < 1) {
                    $.modalAlert("缺少医嘱数据", 'warning');
                    return false;
                }
                $.each(gridsfxmData, function () {    //去掉action
                    for (var i = 0; i < $(this).length; i++) {
                        delete $(this)[i].action;
                    }
                });
                $.najax({
                    url: "@Url.Action("saveAsTemplate")",
                    dataType: "json",
                    data: { mbObj: mbObj, mxList: gridsfxmData },
                    type: "POST",
                    success: function (data) {
                        $.modalMsg("保存成功", 'success');
                        window.$('#current').trigger('click');
                    }
                });
            }
        });
    }
</script>